package com.henu.zmb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.henu.R;
import com.henu.zmb.entity.Goods;
import com.henu.zmb.entity.User_Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;

/**
 * Created by zhumengbo on 2017/5/23.
 */

    public class UserGoods_adapter extends BaseAdapter {
        ArrayList<User_Goods> list = new ArrayList<User_Goods>();
        LayoutInflater inflater;// 布局填充器
        public UserGoods_adapter(Context context ) {
            inflater = LayoutInflater.from(context);// 获取当前上下文的布局填充器
        }
        // 添加数据
        public void addAllData(ArrayList<User_Goods> templist){
            list.clear();
            list.addAll(templist);
        }
        public void addData(ArrayList<User_Goods> templist){
            list.addAll(templist);
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            // 不需要每次更新都获取控件，只在第一次时进行
            if(convertView == null){
                convertView = inflater.inflate(R.layout.activity_goods_adapter, null);//把 xml文件转化为view视图
                holder = new ViewHolder();
                holder.image = (ImageView)convertView.findViewById(R.id.aga_ivimage);
                holder.title = (TextView)convertView.findViewById(R.id.aga_tvtitle);
                holder.time = (TextView)convertView.findViewById(R.id.aga_tvtime);
                holder.price = (TextView)convertView.findViewById(R.id.aga_tvprice);
                holder.update = (Button)convertView.findViewById(R.id.aga_bupdate);
                convertView.setTag(holder);// 将holder放入converView中
            }else{
                holder = (ViewHolder)convertView.getTag();// 获取之前存放的holder
            }
            holder.update.setVisibility(View.GONE);
            //设置当前控件数据（position）
             User_Goods ug = list.get(position);
            Goods g=ug.getGoods_Id();
            if(g.getGoods_Name() != null){
                // 图片显示
                String url = (g.getGoods_Image().getUrl()); // 获取地址
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showImageOnLoading(R.mipmap.ic_launcher)
                        .showImageOnFail(R.mipmap.ic_launcher)
                        .cacheInMemory(true)
                        .cacheOnDisk(true)
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .build();

                ImageLoader.getInstance().displayImage(url, holder.image, options);
                holder.title.setText(g.getGoods_Name());
                holder.time.setText(g.getCreatedAt());
                holder.price.setText("价格:"+ g.getGoods_Price());
            }else{
                holder.title.setText("此物品已不存在，请删除收藏！");

            }

            return convertView;
        }

        class ViewHolder {
            ImageView image;
            TextView title;
            TextView price;
            TextView time;
            Button update;
        }

}

